package com.example.administrator.orchardeveryday.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.example.administrator.orchardeveryday.R;
import com.example.administrator.orchardeveryday.adapter.FruitBaikeListAdapter;
import com.example.administrator.orchardeveryday.adapter.FruitFriendsHeadAdapter;
import com.example.administrator.orchardeveryday.adapter.FruitFriendsLvAdapter;
import com.example.administrator.orchardeveryday.bean.FruitBaikeEntity;
import com.example.administrator.orchardeveryday.bean.FruitFriendsEntity;
import com.example.administrator.orchardeveryday.bean.FruitMain;
import com.example.administrator.orchardeveryday.bean.FruitTop;
import com.example.administrator.orchardeveryday.config.UrlConstants;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;
/**
 * A simple {@link Fragment} subclass.
 */
public class FruitFriendsFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2<ListView>{

    private AsyncHttpClient httpClient=new AsyncHttpClient();
    private  int page;
    private ArrayList<FruitMain> fruitMains=new ArrayList<>();
    private FruitFriendsLvAdapter listAdapter;
    private PullToRefreshListView ptrListView;
    private ArrayList<FruitTop> fruitTops=new ArrayList<>();
    private FruitFriendsHeadAdapter headAdapter;
    private ListView headListView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_fruit_friends, container, false);
        initView(view);
        addHeadView(inflater);
        return view;
    }
    private View headView;
    private void addHeadView(LayoutInflater inflater) {
        headView=inflater.inflate(R.layout.fragment_fruit_friends_head,null);
        headListView= (ListView) headView.findViewById(R.id.fragment_friends_head_listview);
        headAdapter=new FruitFriendsHeadAdapter(fruitTops,getActivity());
        headListView.setAdapter(headAdapter);
        ptrListView.getRefreshableView().addHeaderView(headView);
        ptrListView.setRefreshing();
    }

    private void initView(View view) {
        ptrListView= (PullToRefreshListView) view.findViewById(R.id.fragment_friends_listview);
        ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
        ptrListView.setOnRefreshListener(this);
        listAdapter=new FruitFriendsLvAdapter(fruitMains,getActivity());
        ptrListView.setAdapter(listAdapter);
    }
    private void loadData() {
        httpClient.post(UrlConstants.URL_FRUIT_FRIENDS, getRequestParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
            }
            @Override
            public void onSuccess(int i, Header[] headers, String s) {
                ptrListView.onRefreshComplete();
                FruitFriendsEntity entity = JSONObject.parseObject(s, FruitFriendsEntity.class);
                if (entity != null) {
                    if (page == 1 && !fruitMains.isEmpty()) {
                        fruitTops.clear();
                        fruitMains.clear();

                    }
                    fruitTops.clear();
                    fruitTops.addAll(entity.getTop());
                    headAdapter.notifyDataSetChanged();
                    fruitMains.addAll(entity.getMain());
                    listAdapter.notifyDataSetChanged();
                }
            }
        });
    }
    private RequestParams getRequestParams()
    {
        RequestParams params=new RequestParams();
        params.add("sign", "a58f202c25bb071bf80fcc1708c27319");
        params.add("limit","20");
        params.add("timestamp", "1445431785");
        params.add("platform", "ANDROID");
        params.add("source", "app");
        params.add("page","1");
        params.add("connect_id", "161c2ae532723d329ee711d284a1b36e");
        params.add("region_id", "106092");
        params.add("service", "fruit.getMaxArticleList");
        params.add("device_id", "1b8ba62bf241cee5b8c317d07c0c503e");
        params.add("channel", "portal");
        params.add("version", "3.0.3");
        return params;
    }
    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
        page=1;
        loadData();
    }
    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
        page++;
        loadData();
    }
}
